/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.DragonPokerServer.Classes;

import java.util.ArrayList;
import org.Comunicator.Element;
import org.Comunicator.Global;
import org.Comunicator.Message;
import org.DragonPokerServer.Thread.ThreadUser;

/**
 *
 * @author alien
 */
public class Hand {

    public static final int HAND_START = 0;
    public static final int HAND_FLOP = 1;
    public static final int HAND_TURN = 2;
    public static final int HAND_RIVER = 3;
    public static final int HAND_RESULT = 4;
    private int tableid;
    private int status = 0;
    private ArrayList<HandPlayer> handPlayers = new ArrayList<HandPlayer>();
    private Deck deck = new Deck();
    private ArrayList<Card> cards = new ArrayList<Card>();

    public Hand() {
    }

    public Hand(ArrayList<Player> players) {
        int dealer = 0;
        int check = 0;
        for (int i = 0; i < players.size(); i++) {
            if (players.get(i).isDealer()) {
                dealer = i;
                break;
            }
        }

        for (int i = dealer + 1; i != dealer; i++) {
            if (i == players.size() - 1) {
                i = 0;
                check = 1;
            }

            if (check == 1) {
                HandPlayer hp = new HandPlayer(players.get(players.size() - 1));
                handPlayers.add(hp);
            }
            if (!players.get(i).isDealer()) {
                HandPlayer hp = new HandPlayer(players.get(i));
                handPlayers.add(hp);
            } else {
                break;
            }

            check = 0;
        }
        HandPlayer hp = new HandPlayer(players.get(dealer));
        handPlayers.add(hp);

    }

    private boolean start() {
        for(HandPlayer hp: handPlayers){
            hp.getCards().add(deck.getCard());
            hp.getCards().add(deck.getCard());
            Message reply = new Message();
            reply.setAction("PlayerCards");
            reply.getReceiver().add(hp.getPlayer().getThreadUser().getUser().getUsername());
            reply.getParameters().add(new Element("TableId", hp.getCards().get(0)));
            reply.getParameters().add(new Element("Card", hp.getCards().get(0)));
            reply.getParameters().add(new Element("Card", hp.getCards().get(1)));
            Global.messages.addMessageToSend(reply);
        }

        setStatus(1);
        return true;
    }

    private boolean flop() {
        ArrayList<Card> alc = deck.getDeck();
        for(HandPlayer hp: handPlayers){
            Message reply = new Message();
            reply.setAction("FlopCards");
            reply.getReceiver().add(hp.getPlayer().getThreadUser().getUser().getUsername());
            reply.getParameters().add(new Element("TableId", hp.getCards().get(0)));
            reply.getParameters().add(new Element("Card", alc.get(0)));
            reply.getParameters().add(new Element("Card", alc.get(1)));
            reply.getParameters().add(new Element("Card", alc.get(2)));
            Global.messages.addMessageToSend(reply);
        }
        cards.add(alc.get(0));
        cards.add(alc.get(1));
        cards.add(alc.get(2));

        setStatus(2);
        
        return true;
    }

    private boolean turn() {
                Card c = deck.getTurnRiver();
        for(HandPlayer hp: handPlayers){
            Message reply = new Message();
            reply.setAction("TurnCard");
            reply.getReceiver().add(hp.getPlayer().getThreadUser().getUser().getUsername());
            reply.getParameters().add(new Element("TableId", hp.getCards().get(0)));
            reply.getParameters().add(new Element("Card", c));
            Global.messages.addMessageToSend(reply);
        }
        cards.add(c);
        setStatus(3);
        return true;
    }

    private boolean river() {
                Card c = deck.getTurnRiver();
        for(HandPlayer hp: handPlayers){
            Message reply = new Message();
            reply.setAction("RiverCard");
            reply.getReceiver().add(hp.getPlayer().getThreadUser().getUser().getUsername());
            reply.getParameters().add(new Element("TableId", hp.getCards().get(0)));
            reply.getParameters().add(new Element("Card", c));
            Global.messages.addMessageToSend(reply);
        }
        cards.add(c);
        setStatus(4);
        return true;
    }

    private boolean result() {

        return false;
    }

    public void nextAction() {
        switch (status) {
            case 0:
                start();
                break;
            case 1:
                flop();
                break;
            case 2:
                turn();
                break;
            case 3:
                river();
                break;
            case 4:
                result();
                break;
            default:
                break;
        }
    }

    public Deck getDeck() {
        return deck;
    }

    public void setDeck(Deck deck) {
        this.deck = deck;
    }

    public ArrayList<HandPlayer> getHandPlayers() {
        return handPlayers;
    }

    public void setHandPlayers(ArrayList<HandPlayer> handPlayers) {
        this.handPlayers = handPlayers;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public int getTableid() {
        return tableid;
    }

    public void setTableid(int tableid) {
        this.tableid = tableid;
    }


}
